var sforce =  sforce == undefined ? {}:sforce;

var EUITempleUtil = {
            back:function(isSuc,msg,backId){

                if(msg)alert(msg);
                if(isSuc){
                    var backurl = getURLParam().get('backurl');
                    if(!backurl)backurl = '/'+backId;
                    if(backId){
                        if(sforce.one){
                            sforce.one.navigateToURL(backurl);
                        }else{
                            window.location.herf = backurl;
                        }
                    }else{
                        window.location.go(-1);
                    }
                }
            },
            getURLParam:function(){
                var url = location.search+'';
                var astr = url.substr(1);  //去掉'?'
                var atemp = new Array();
                atemp = astr.split('&');

                var amap = new Map();
                for(var i=0 ;i<atemp.length; i ++){
                    var ai = atemp[i];
                    if(ai.indexOf('=') >=0){
                        var atp = ai.split('=');
                        amap.set(atp[0],decodeURIComponent(atp[1]));
                    }       
                }
                return amap;
            },
    		referenceObjects:[],
            searchObjects :[],
    		limit:10,

    		//labelWidth:window.screen.availWidth > 1000  ? 240 : 200 ,
    		//width:window.screen.availWidth > 1000  ? 500 : 400 ,
            
    		currentField :null,
            searchClickRow:null,
            
    		initBox:function(field,isMobile,isSearch ){
    			if(!isSearch){

    				if(field.type =='reference'){
    					/*var tmp = this.referenceObjects[field.reference.objname]  == undefined ? [] : this.referenceObjects[field.reference.objname] ;
    					
    					tmp.push(field.reference);*/

    					this.referenceObjects[field.reference.objname ]  = [field.reference] ;
    				}
    			}else{

                   var tmp =  this.searchObjects[field.objname] == undefined ? [] : this.searchObjects[field.objname];
                   tmp.push(field);
                    this.searchObjects[field.objname] = tmp ; 
                }
    			
    			if(isSearch){
    				if(isMobile){
    					this.labelWidth = 'auto';
    					this.width = window.screen.availWidth -100;
    				}else{
    					this.width =(window.screen.availWidth  ) * 0.4;
                        this.labelWidth=(window.screen.availWidth  ) * 0.4* 0.4;
    				}
    			}else{
                    
                    this.width =(window.screen.availWidth - 100 ) * 0.5;
                    this.labelWidth=(window.screen.availWidth - 100 ) * 0.5* 0.4;
                }
                if(isMobile) this.labelWidth = window.screen.availWidth -60;
                if(isMobile)this.width = window.screen.availWidth+this.labelWidth- 60;
    			 
    			if(field.type == 'date'  ) {

    				 this.initDateBox(field,isMobile,isSearch);

    			}else if( field.type == 'datetime'){

                    this.initDateTimeBox(field,isMobile,isSearch); 

                }else if(field.type == 'currency' || field.type =='double' ||field.type == 'percent' ||field.type == 'integer') {

    				this.initNumerbox(field,isMobile,isSearch);
    			
    			}else if(field.type == 'picklist'){

    				 this.initCombobox(field,isMobile,isSearch);

				}else if(field.type == "multipicklist"){
				 	
				 	this.initCombobox(field,isMobile,isSearch,true);

				}else if(!isSearch && field.type == 'reference'){

					this.initSearchbox(field,isMobile,isSearch);

				}else{


					this.initTextBox(field,isMobile,isSearch);
				} 
    			

    		},

            invokeMethod:function(field,method,arg ){

                try{
                    var tagId = field.type == 'reference' ?  field.objname +"-"+field.name+"-Name" :  field.objname +"-"+field.name;
                
                    var tag = document.getElementById(tagId);

                    if(field.type == 'date'  ) {

                        if(arg){
                            $(tag).datebox(method,arg);
                        }else{
                            $(tag).datebox(method);
                        }
                        

                    }else if( field.type == 'datetime'){
                        if(arg){
                            $(tag).datetimebox(method,arg);
                        }else{
                            $(tag).datetimebox(method);
                        }


                    }else if(field.type == 'currency' || field.type =='double' ||field.type == 'percent' ||field.type == 'integer') {
                        if(arg){
                            $(tag).numberbox(method,arg);
                        }else{
                            $(tag).numberbox(method);
                        }
                    
                    }else if(field.type == 'picklist'|| field.type == "multipicklist" ){
                        if(arg){
                            $(tag).combobox(method,arg);
                        }else{
                            $(tag).combobox(method);
                        }

                    }else if(field.type == 'reference'){

                        if(arg){
                            $(tag).searchbox(method,arg);
                        }else{
                            $(tag).searchbox(method);
                        }

                    }else{

                        if(arg){
                            $(tag).textbox(method,arg);
                        }else{
                            $(tag).textbox(method);
                        }
                    } 

                }catch(e){}
                
            },

    		getFieldInput:function (field,isSearch){

    			var tagId = field.objname +"-"+field.name;

    			if(isSearch){
    				return ' <input id="'+tagId+'" /> ';
    			}else{

    			}
	    		return field.type == 'reference' ? '<input id="'+tagId+'-Name" /><input class="'+field.reference.objname+'" '+(field.parents != null ? 'parentSobject="'+ field.parents.join("-") + '"': "")+' type="hidden" id="'+tagId +'" /> <input type="hidden" id="'+tagId+'-RecordType"/> '+ (field.reference.objname == 'Account' ? '<input type="hidden" id="'+tagId+'-Third"/>':'') : '<input id="'+tagId+'" /> ';
	    	},

    		initSearchbox:function(field,isMobile){
    			$("#"+field.objname +"-"+field.name+"-Name").searchbox({
					width: this.width,
					label: field.label,
					labelWidth:this.labelWidth,
					labelAlign:isMobile ? 'left':'right',
					value:field.namevalue,
                    required:field.required ? true : false,

					searcher:function(value,name ){
						//console.log(value +" "+name );
                        EUITempleUtil.gensearchObjectsSqlCustom = null ;
                        EUITempleUtil.searchClickRow = null;
						EUITempleUtil.currentField = field;
                        field.reference.value = field.value.Name;
						EUITempleUtil.initSearchDialog(this,field.reference.objname,[field.reference],[field.reference],'field',10,isMobile);
						//EUITempleUtil.setboxValue(field.reference,field.value.Name);

					}
				});
				//$("#"+field.objname +"-"+field.name).attr('objname',field.objname)
				$("#"+field.objname +"-"+field.name).val(field.value.Id);
				$("#"+field.objname +"-"+field.name+"-Name").searchbox('setValue',field.value.Name);
                $("#"+field.objname +"-"+field.name+"-RecordType").val(field.value.RecordType);
                if(field.reference.objname == 'Account')$('#'+field.objname +"-"+field.name+"-Third" ).val(field.value.Third);

    		},
    		//生成textbox 
    		initTextBox:function(field  ,isMobile,isSearch){
               // console.log(this.width);
               // console.log(this.labelWidth);
    			var tagId = field.objname +"-"+field.name ;
                var tag = document.getElementById(tagId);
    			$(tag).textbox({
					width: this.width,
					label: field.label,
					labelWidth:this.labelWidth,
					labelAlign:isMobile ? 'left':'right',
                    multiline: field.type =='textarea',
                    height: field.type == 'textarea' ? 44: undefined,
                    required:field.required ? true : false,
				});
				if(!isSearch) $("#"+tagId).textbox({value:field.value});

    		},

    		initDateBox:function(field,isMobile,isSearch){
    			var tagId = field.objname +"-"+field.name ;
    			$("#"+tagId).datebox({
					width: this.width,
					label: field.label,
					formatter:this.dateFormatter,
					labelWidth:this.labelWidth,
					labelAlign:isMobile ? 'left':'right',
                    required:field.required ? true : false,
					parser:this.dateParser
				});
				if(!isSearch)$("#"+tagId).datebox({value: field.value});
				$("#"+field.objname +"-"+field.name).focus(function(){
				      document.activeElement.blur();
				});
    		},
            initDateTimeBox:function(field,isMobile,isSearch){
                var tagId = field.objname +"-"+field.name ;
                $("#"+tagId).datetimebox({
                    width: this.width,
                    label: field.label,
                    showSeconds: true,
                    formatter:this.dateTimeFormatter,
                    labelWidth:this.labelWidth,
                    labelAlign:isMobile ? 'left':'right',
                    required:field.required ? true : false,
                    parser:this.dateTimeParser
                });
                if(!isSearch)$("#"+tagId).datetimebox({value: field.value});
                $("#"+field.objname +"-"+field.name).focus(function(){
                      document.activeElement.blur();
                });
            },
    		initCombobox:function(field,isMobile,isSearch,isMult){

    			if(isMult == null || isMult == undefined) isMult = false;
    			var tagId = field.objname +"-"+field.name ;
    			$('#'+tagId ).combobox({
					width: this.width,
				    valueField:'id',
				    textField:'text',
                    required:field.required ? true : false,

				    label:field.label,
				    data:field.data,
					labelAlign:isMobile ? 'left':'right',
				    limitToList:true,
                     panelHeight:'auto',
				    panelMaxHeight:isMobile ? 160 : 200 ,
					labelWidth:this.labelWidth,
				    multiple:isMult


				});
				if(!isSearch)$("#"+tagId).combobox({value: field.value});
				$("#"+tagId ).focus(function(){
				      document.activeElement.blur();
				});


    		},
    		initNumerbox:function(field,isMobile,isSearch){
    			
    			var tagId = field.objname +"-"+field.name ;
    			$('#'+tagId).numberbox({
					width: this.width,
				    label:field.label,
				    labelWidth:120,
					labelAlign:isMobile ? 'left':'right',
				    precision:2,
                    required:field.required ? true : false,
				    labelWidth:this.labelWidth,
				});
				if(!isSearch)$("#"+tagId).numberbox({value: field.value});
    		},
    		
    		setboxValue:function(field,value){
    			//console.log(field );
                //console.log(value);
    			if(field.type == 'date') {

    				return $('#'+field.objname +"-"+field.name ).datebox("setValue",value);

    			}else if(field.type == 'currency' || field.type =='double' ||field.type == 'percent' ||field.type == 'integer') {

    				return $('#'+field.objname +"-"+field.name ).numberbox("setValue",value);
    			
    			}else if(field.type == 'picklist'){

    				return $('#'+field.objname +"-"+field.name ).combobox("setValue",value);

				}else if(field.type == "multipicklist"){
				 	
    				 $('#'+field.objname +"-"+field.name ).combobox("setValues",value == null ? "" :value);
				
				}else if(field.type == "reference" ){
    				if(value != undefined && value != null ){
                        //console.log(field.objname +"-"+field.name);
                        $('#'+field.objname +"-"+field.name+"-Name" ).searchbox("setValue",value.Name );
                        $('#'+field.objname +"-"+field.name ).val(value.Id  );
                        $('#'+field.objname +"-"+field.name+"-RecordType" ).val(value.RecordType  );
                    } 
    				
                     $('#'+field.objname +"-"+field.name ).trigger('input:changed',value);
                     if(field.reference.objname == 'Account'){
                        $('#'+field.objname +"-"+field.name+"-Third" ).val(value.Third);
                     }


				}else{

    				return $('#'+field.objname +"-"+field.name ).textbox("setValue",value);
				}
    		},          
    		getboxValue:function(field){

    			////console.log(field.objname +"-"+field.name);
    			var tagId = field.objname +"-"+field.name;
    			if(field.type == 'date') {

    				return $(document.getElementById(tagId) ).datebox("getValue");

    			}else if(field.type == 'datetime') {

                    return $(document.getElementById(tagId) ).datetimebox("getValue");

                }else if(field.type == 'currency' || field.type =='double' ||field.type == 'percent' ||field.type == 'integer') {

    				return $(document.getElementById(tagId)).numberbox("getValue");
    			
    			}else if(field.type == 'picklist'){

    				return $(document.getElementById(tagId)).combobox("getValue");

				}else if(field.type == "multipicklist"){
				 	
    				return $(document.getElementById(tagId)).combobox("getValues").join(";");
				
				}else if(field.type == "reference"){
					return $(document.getElementById(tagId)).val();
				}else{

    				return $(document.getElementById(tagId) ).textbox("getValue");
				} 
    		},
            dateTimeFormatter:function(date){
               
                 var y = date.getFullYear();
                var m = date.getMonth()+1;
                var d = date.getDate();
                var h = date.getHours();
                var min = date.getMinutes();
                var sec = date.getSeconds();

                return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d) +' '+(h<10?('0'+h):h)+':'+(min<10?('0'+min):min)+':'+(sec<10?('0'+sec):sec) ;
            },
            dateTimeParser:function (s){
                if (!s) return new Date();
                try{

                    return new Date(s);

                }catch(e){

                    var datetime = (s.split(' '));
                    var dates = datetime[0].split('-');
                    var times = datetime[1].split(':');
                    var y = parseInt(dates[0],10);
                    var m = parseInt(dates[1],10);
                    var d = parseInt(dates[2],10);
                    var h = parseInt(times[0],10);
                    var min = parseInt(times[1],10);
                    var sec = parseInt(times[2],10);
                    if (!isNaN(y) && !isNaN(m) && !isNaN(d) && !isNaN(h) && !isNaN(min) && !isNaN(sec)){
                        return new Date(y,m-1,d,h,min,sec);
                    } else {
                        return new Date();
                    }

                }

               
            },
    		dateFormatter:function(date){
               
    			 var y = date.getFullYear();
	            var m = date.getMonth()+1;
	            var d = date.getDate();

	            return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    		},
    		dateParser:function (s){
	            if (!s) return new Date();
	            var ss = (s.split('-'));
	            var y = parseInt(ss[0],10);
	            var m = parseInt(ss[1],10);
	            var d = parseInt(ss[2],10);
	            if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
	                return new Date(y,m-1,d);
	            } else {
	                return new Date();
	            }
	        },
            gensearchObjectsSqlCustom:null,
	        gensearchObjectsSql:function(objectname,isRecordType ,targerFeilds,searchFeilds,limit ){
	        	if(limit == null || limit == undefined) limit =  10 ;
	        	var ss = [];
	        	var isHasId = false;
	        	var exFmap = [];
               // console.log(targerFeilds);
	        	for(var i = 0 ; i < targerFeilds.length ; i  ++){

	        		if(targerFeilds[i].name == 'Id') isHasId = true;
	        		if(exFmap[targerFeilds[i].name] == undefined){

                        ss.push(targerFeilds[i].name );

                    }
	        		exFmap[targerFeilds[i].name] = targerFeilds[i].name;
	        		
	        	}
                ////console.log(isRecordType);
	        	var  sql = ' select ' +(isHasId ? '' : ' Id, ') +(isRecordType ? ' RecordType.DeveloperName ,':' ') +(objectname == 'Account' ? ' System__c , ':' ')+ss.join(',') + ' from '+objectname + ' ';
	        	if(searchFeilds == null || searchFeilds.length == 0){
	        	 
	        	}else{
	        		var t = 0 ; 
	        		for(var i = 0 ; i <searchFeilds.length ; i ++){
	        			if(searchFeilds[i].value == "" ||searchFeilds[i].value == undefined || searchFeilds[i].value == null  ) continue; 
	        			if(t == 0 ){
	        				sql += ' where '
	        			}
	        			if( t != 0 ) sql += ' and ';
	        			sql +=  this.genSqlCondition(searchFeilds[i]) ;
	        			t ++;
	        		}

	        	}
                if(this.gensearchObjectsSqlCustom != null ){

                    var tmp = '';
                    if(sql.indexOf('where') === -1 &&  this.gensearchObjectsSqlCustom.indexOf('and') >= 0 ){
                       
                        tmp = this.gensearchObjectsSqlCustom.replace('and',' ');
                    }
                    if(sql.indexOf('where') === -1) sql += ' where ';
                    if(tmp === '') tmp = this.gensearchObjectsSqlCustom + ' ';
                    sql += tmp  ; 
                } 

	        	sql += ' limit '+ limit;
              // console.log(sql);

	        	return sql;


	        },
	        genSqlCondition:function (field){

	        	if(field.type == 'picklist' || field.type == 'reference' ) {

	        		return field.name +" = '"+field.value+"' ";

	        	}else if(field.type == 'multipicklist'){
	        		if(field.value != null && field.value.length > 0){
	        			return field.name +" includes ('"+field.value.join("','")+"') ";
	        		}
	        		return field.name +" includes () ";

	        	}else if(field.type == 'currency' || field.type =='double' ||field.type == 'percent' ||field.type == 'integer'){
	        		return field.name +" = "+field.value+" ";
	        	}else if(field.type == 'date' || field.type == 'datatime' ||  field.type == 'time' ){

	        		return  field.name +" "+ field.operater +" "+field.value+" ";
	        	}else {

	        		return field.name + " like '%"+field.value+"%' "; 
	        	}
	        },
	        queryObject:function(sql){

	        	ImportObject.query(sql ,function(result,event){
                        if(window.dismissLoading != undefined)dismissLoading();
		                if(event.status == true){
                           // console.log(result.data);
		                	if(result != null && result.data != null )$("#SearchObjectTable").datagrid("loadData",{rows:result.data,total:result.data.length});
		                }else{

		                }    
		        
		            });
    	      
	        },
		    search_btnClick:function (e){
                 if(window.showLoadingMsg != undefined)showLoadingMsg('正在检索...');

		    	//获取值
               
		    	var searchFields = this.referenceObjects[(e.id.split("-")[0])];
		    	for(var i = 0 ; i <searchFields.length ; i ++){
                    try{
                        searchFields[i].value = this.getboxValue(searchFields[i]);
                    }catch(e){}
		    		
		    	}
                ////console.log(searchFields);
                ////console.log(searchFields[0].isRecordtype);
		    	var sql = this.gensearchObjectsSql(searchFields[0].objname,searchFields[0].isRecordtype ,searchFields,searchFields,10);
		    	console.log(sql);
		    	this.queryObject(sql);
    		},
            search_detail_btnClick:function(e){
               // //console.log(e);
                 if(window.showLoadingMsg != undefined)showLoadingMsg('正在检索...');

                var searchFields = this.searchObjects[(e.id.split("-")[0])];
                for(var i = 0 ; i <searchFields.length ; i ++){
                    searchFields[i].value = this.getboxValue(searchFields[i]);
                }
                ////console.log(searchFields);
                if(search_detail != undefined)search_detail(e,searchFields);
            },
    		initSearchDialog: function (tag ,objname,searchFeilds, targerFeilds,type,limit,isMobile ,callback ){
        		//1.搜索字段
        		//2.目标字段 
        		//3.搜索对象
        		//4.搜索条数

        		//1.生成html 
        		var data  = [];
        		data.push('<div id="PL_SearchDialog">');
    			//4.搜索条数
        		data.push(this.initSearchTable(searchFeilds,objname,type,isMobile,callback));

        		data.push('<table id="SearchObjectTable" />');



        		data.push("</div>");
        		var html = data.join("");
        		//console.log(html);
        		//console.log($(tag).parents('div'));
        		$("#dialog_content").append(html);

        		//3.选中回调
    		
    			//2.初始化
        		var width = isMobile ? window.screen.availWidth - 80: window.screen.availWidth - 120;
                var height = isMobile ? window.screen.availHeight - 120 : window.screen.availHeight - 200;
        		$("#PL_SearchDialog").dialog({
        			width:width,
        			height:height,
        			resizable:true,
        			modal:true,
        			closed:true,
        			title:"选择",
        			onClose:function(){
        				$(this).remove();
        			}

        		});


        		//2.1初始搜索条件
                EUITempleUtil.searchObjects = [];
        		for(var i = 0 ; i <searchFeilds.length; i ++){

        		 	EUITempleUtil.initBox(searchFeilds[i],isMobile,true);
                    EUITempleUtil.setboxValue(searchFeilds[i],searchFeilds[i].value);
        		}
        		//2.2 初始化 结果表格

        		var columns = [];
        		for(var i = 0 ; i <targerFeilds.length ; i ++){
        			var field = targerFeilds[i] ; 
                    var cl = {field:field.name,title:field.label,width:field.width,formatter:field.formatter,editor:field.editor};
                     if(field.type == "reference"){
                        cl.formatter = function(value,row,index){
                           
                            if(this.field.indexOf(".") > 0 ){

                                var fcmp = this.field.split(".");
                                var val = row;
                                for(var j = 0 ; j <fcmp.length ; j ++){
                                    if(val != undefined)val = val[fcmp[j]];
                                }

                                return val ; 
                            }
                        }
                     }
        			columns.push(cl);
        		}
        		$("#SearchObjectTable").datagrid({

        			width:width -20 ,//'auto',
                    height: height -40,//isMobile ?  200 : 400 ,
                    columns:[columns],
                    toolbar:"#PL_search_toolbar",
                   
        		});
                /*toolbar: [{
                                text:'确认选择',
                                iconCls:'icon-save',
                                handler:function(){

                                    if(callback != null ) callback(this);
                                    $('#PL_SearchDialog').dialog('close');                             
                                    
                                }
                            }]*/
        		if(type == 'detail'){
        			$("#SearchObjectTable").datagrid({
        				frozenColumns:[[  
    	                    {field:'ck',checkbox:true}  
    	                ]],
        				
    				});
    			}else{
    				$("#SearchObjectTable").datagrid({
    					onClickRow: function (rowIndex, rowData) {            

                            if(EUITempleUtil.searchClickRow!= null   ){
                               
                                EUITempleUtil.searchClickRow(rowIndex,rowData);
                                $('#PL_SearchDialog').dialog('close');
                                return;
                            }
    					  
                           // //console.log({Name:rowData.Name ,Id:rowData.Id,RecordType: rowData.RecordType == undefined ? '' : rowData.RecordType.DeveloperName });
                           var data = {Name:rowData.Name ,Id:rowData.Id,RecordType: rowData.RecordType == undefined ? '' : rowData.RecordType.DeveloperName } ;
                           if(EUITempleUtil.currentField.reference.objname == 'Account') data.Third = rowData.System__c;
    					  	EUITempleUtil.setboxValue(EUITempleUtil.currentField,data);
    					    $('#PL_SearchDialog').dialog('close');
    	                }
    	            })
    			}
    				$('#PL_SearchDialog').dialog('open').dialog('center');

        	},

        	initSearchTable:function (fields,objname,type,isMobile,callback ){

        		var data = [];
                data.push('<div id="PL_search_toolbar" style="max-height:200px;overflow-y:scroll">');
        		data.push('<table  >');
        		
        			var trs = isMobile ? fields.length : Math.ceil(fields.length / 2 );
        		//	//console.log(trs );
    	    		for(var i = 0 ; i < trs ; i ++){
    	    			data.push('<tr>');
    				   if(isMobile){
    				   	 	data.push('<td >'+EUITempleUtil.getFieldInput(fields[i],true )+'</td>')
    				   }else{
    				   	 for(var j = 0 ; j < 2 ; j ++){
    				   		 if((i * 2+j) <fields.length)data.push('<td>'+EUITempleUtil.getFieldInput(fields[(i * 2+j)],true)+'</td>');
    				   	 }

    				   }
    	    			data.push('</tr>');

    	    		}
                    if(fields != null && fields.length > 0){
                        if(type == 'field'){
                            data.push('<tr><td><a class="search_btn" id="'+objname+'-Search" onclick="EUITempleUtil.search_btnClick(this)" >检索</a></td></tr>');
                        }else{
                            data.push('<tr><td><a class="search_btn" id="'+objname+'-Search" onclick="EUITempleUtil.search_detail_btnClick(this)" >检索</a></td></tr>');
                        }

                    }
                    

                    if(type == 'detail'){
                        data.push('<tr><td><a class="search_btn"  onclick="javascript:{ if(search_detail_confirmclick(this))$(\'#PL_SearchDialog\').dialog(\'close\')}" >确认选择</a></td></tr>');
                    }

    	    		//if(!isMobile)data.push('<td />');

        			data.push("</table>");
                    data.push('</div >');
        		 	var table  = data.join("");
        		 	////console.log(table);

        		 return table;

        	},

            initBaseTableWithGroup:function (groupsMap) {

                /*
                    [
                        {
                            title: '',
                            fields:[],
                        },

                    ]
                */

                var tmp  = [];
                var isMobile = (sforce.one != undefined);
                var data = [];
                data.push('<table >');
                for (var i = 0 ;i < groupsMap.length ; i ++ ) {
                    var fields = groupsMap[i].fields;
                    var trs = isMobile ? fields.length : Math.ceil(fields.length / 2 );

                    data.push('<tr><td class="group_title" '+(isMobile ? '' : ' colspan="2" ') +'>'+groupsMap[i].title+'</td></tr>');

                     tmp = tmp.concat(fields );

                    for(var k = 0 ; k < trs ; k ++){
                        data.push('<tr>');

                       if(isMobile){

                            data.push('<td >'+EUITempleUtil.getFieldInput(fields[k])+'</td>')

                       }else{

                         for(var j = 0 ; j < 2 ; j ++){

                             if((k * 2+j) <fields.length)data.push('<td>'+EUITempleUtil.getFieldInput(fields[(k * 2+j)])+'</td>');
                         }

                       }
                        data.push('</tr>');

                    }

                }

                 data.push("</table>");
                 var table  = data.join("");
               //  //console.log(table);
                $("#baseInfo_table").html(table);

                 for(var i = 0 ; i <tmp.length; i ++){
                    EUITempleUtil.initBox(tmp[i],isMobile);
                 }

            },
            initBaseTable:function (fieldsMap,titleMap ,tag ) {

                var isMobile = (sforce.one != undefined);
                var data = [];

                data.push('<table >');
                var objs = Object.keys(titleMap) ;
               // //console.log(objs);
                var tmp  = [];
                for(var k= 0 ; k<objs.length ; k ++  ){
                    var fields = fieldsMap[objs[k]]
                    tmp = tmp.concat(fields );
                    var trs = isMobile ? fields.length : Math.ceil(fields.length / 2 );
                    data.push('<tr><td class="base_title" '+(isMobile ? '' : ' colspan="2" ') +'>'+titleMap[objs[k]].title+'</td></tr>');
                    for(var i = 0 ; i < trs ; i ++){
                        data.push('<tr>');
                       if(isMobile){
                            data.push('<td >'+EUITempleUtil.getFieldInput(fields[i])+'</td>')

                       }else{

                         for(var j = 0 ; j < 2 ; j ++){
                             if((i * 2+j) <fields.length)data.push('<td>'+EUITempleUtil.getFieldInput(fields[(i * 2+j)])+'</td>');
                         }

                       }
                        data.push('</tr>');

                    }
                }

                data.push("</table>");
                 var table  = data.join("");
               //  //console.log(table);
                if(!tag)$("#baseInfo_table").html(table);
                if(tag)$(tag).html(table);

                 for(var i = 0 ; i <tmp.length; i ++){
                    EUITempleUtil.initBox(tmp[i],isMobile);
                 }
            }	

    	}